package shopcomputer;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



@WebServlet("/addcomputer")
public class AddComputerServlet extends HttpServlet {

	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		response.getWriter().append("Served at:").append(request.getContextPath());
	
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		
		String brand = request.getParameter("brand");
		String color = request.getParameter("color");
		double price = Double.parseDouble(request.getParameter("price"));
		
		Computer com = new Computer(brand, color, price);
		
		HttpSession session = request.getSession();
		Object object = session.getAttribute("myComputer");
		Set<Computer> comp;
		
		if(object == null) {
			comp = new HashSet<Computer>();
			comp.add(com);
			session.setAttribute("myComputer", comp);
		}else {
			comp = (Set<Computer>)object;
			comp.add(com);
			session.setAttribute("myComputer", comp);
		}
		
		
		response.sendRedirect("computer.html");
		
	
	
	}
	
	
	
	
}














